package com.sz.common.base.entity;

import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: I18nEntity <br>
 * Author: Charles <br>
 * Date: 2016-12-10 00:25:00
 */
public class I18nEntity extends EntityBase {
    private String tableName;
    private String fieldNameRefId = "refId";
    private String fieldNameLanguage = "language";

    private Serializable refId;
    private String language;

    private Map<String, String> i18nFields = new HashMap<>();

    /**
     * @param refId     --reference Id
     * @param language  --language code
     * @param tableName --table name
     */
    public I18nEntity(Serializable refId, String language, String tableName) {
        this(refId, language, tableName, null, null);
    }

    /**
     * @param refId              --reference Id
     * @param language           --language code
     * @param tableName          --table name
     * @param fieldNameRefId    --reference Id filedName
     * @param fieldNameLanguage --language code filedName
     */
    public I18nEntity(Serializable refId, String language,
                      String tableName, String fieldNameRefId, String fieldNameLanguage) {
        this.tableName = tableName;
        this.refId = refId;
        this.language = language;

        if (fieldNameRefId != null && fieldNameRefId.trim().length() > 0) {
            this.fieldNameRefId = fieldNameRefId;
        }
        if (fieldNameLanguage != null && fieldNameLanguage.trim().length() > 0) {
            this.fieldNameLanguage = fieldNameLanguage;
        }
    }

    /**
     * 获取表名称
     *
     * @return
     */
    public String getTableName() {
        return tableName;
    }

    public Object getRefId() { // return type must be Object
        return refId;
    }

    public void setRefId(Serializable refId) {
        this.refId = refId;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getFieldNameRefId() {
        return fieldNameRefId;
    }

    public String getFieldNameLanguage() {
        return fieldNameLanguage;
    }

    /**
     * @param fieldName -- i18n field name
     * @param value     -- i18n value
     */
    public I18nEntity setFieldValue(String fieldName, String value) {
        if (fieldName == null || fieldName.trim().length() == 0) {
            throw new IllegalArgumentException("field name cannot be empty");
        }
        if (value == null || value.length() == 0) { // set empty string is not allowed, see MySQL function 'COALESCE'
            value = null;
        }
        this.i18nFields.put(fieldName, value);
        return this;
    }

    /**
     * 获取所有字段名称，用于create
     *
     * @return
     */
    public List<String> getFieldNames() {
        List<String> list = new ArrayList<>();
        list.add(this.fieldNameRefId);
        list.add(this.fieldNameLanguage);
        this.i18nFields.keySet().forEach(key -> {
            if (i18nFields.get(key) != null) { //cannot set null when create
                list.add(key);
            }
        });
        return list;
    }

    /**
     * 获取所有字段值，用于create
     *
     * @return
     */
    public List<Object> getFieldValues() {
        List<Object> list = new ArrayList<>();
        list.add(this.refId);
        list.add(this.language);
        this.i18nFields.keySet().forEach(key -> {
            if (i18nFields.get(key) != null) { //cannot set null when create
                list.add(i18nFields.get(key));
            }
        });
        return list;
    }

    /**
     * 获取所有i18n字段
     *
     * @return
     */
    public Map<String, String> getI18nFields() {
        return this.i18nFields;
    }

    public boolean hasI18nValue() {
        Map<String, String> i18nFields = this.getI18nFields();
        for (String value : i18nFields.values()) {
            if (StringUtils.hasLength(value)) {
                return true;
            }
        }
        return false;
    }
}
